package com.haois.mp.wechat.common;

import com.haois.mp.database.module.common.daomain.TempMessage;
import com.haois.mp.database.module.wxserver.util.TempMsgUtil;
import com.haois.mp.database.util.HttpClientUtil;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import com.haois.mp.database.module.basic.util.SpringContextHolder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by RuiHaoZ on 2018/7/14 13:26
 */
@Service
public class BusinessRemind {

    /**
     * 关注状态变更模板消息推送
     *
     * @param wxMpService
     * @param wxMpUser    微信用户
     * @param openId      目标用户openId
     */
    public static void remindSubByWx(WxMpService wxMpService, WxMpUser wxMpUser, String openId) {
        try {
            String[] temp = new String[]{wxMpUser.getOpenId(), wxMpUser.getNickname(), "关注状态变更", wxMpUser.getSubscribe() ? "微信用户关注了公众号" : "微信用户取消关注公众号"};
            remindByWx(wxMpService, temp, openId, TempMsgUtil.title_Sub, TempMsgUtil.temp_Sub, TempMsgUtil.temp_Sub_Color, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 系统错误提醒
     *
     * @param toUser    触发错误的用户
     * @param content   内容
     * @param openId    目标用户openId
     * @param className 触发错误的类名
     * @param url       显示详情的URL
     */
    public static void remindErrorByWx(String toUser, String content, String openId, String className, String url) {
        WxMpService wxMpService = SpringContextHolder.getBean(WxMpService.class);
        try {
            String[] temp = new String[]{toUser, className, "系统错误提醒", content};
            remindByWx(wxMpService, temp, openId, TempMsgUtil.title_Sub, TempMsgUtil.temp_Sub, TempMsgUtil.temp_Sub_Color, url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 系统错误提醒
     *
     * @param toUser    触发错误的用户
     * @param content   内容
     * @param openId    目标用户openId
     * @param className 触发错误的类名
     */
    public static void remindErrorByWx(String toUser, String content, String openId, String className) {
        remindErrorByWx(toUser, content, openId, className, null);
    }


    /**
     * 通用模板
     *
     * @param tempData  模板数据
     * @param openId    目标用户OpenId
     * @param title     模板标题
     * @param tempField 模板字段定义
     * @param tempColor 模板颜色
     * @throws WxErrorException
     */
    public static void remindByWx(String[] tempData, String openId, String title, String[] tempField, String[] tempColor) throws WxErrorException {
        remindByWx(tempData, openId, title, tempField, tempColor, null);
    }

    /**
     * 通用模板
     *
     * @param tempData  模板数据
     * @param openId    目标用户OpenId
     * @param title     模板标题
     * @param tempField 模板字段定义
     * @param tempColor 模板颜色
     * @param url       展示详情URL
     * @throws WxErrorException
     */
    public static void remindByWx(String[] tempData, String openId, String title, String[] tempField, String[] tempColor, String url) throws WxErrorException {
        WxMpService wxMpService = SpringContextHolder.getBean(WxMpService.class);
        remindByWx(wxMpService, tempData, openId, title, tempField, tempColor, url);
    }


    /**
     * 通用模板
     *
     * @param wxMpService
     * @param tempData    模板数据
     * @param openId      目标用户OpenId
     * @param title       模板标题
     * @param tempField   模板字段定义
     * @param tempColor   模板颜色
     * @param url         展示详情URL
     * @throws WxErrorException
     */
    public static void remindByWx(WxMpService wxMpService, String[] tempData, String openId, String title, String[] tempField, String[] tempColor, String url) throws WxErrorException {
        Map<String, String> mapData = new HashMap(tempData.length);
        for (int i = 0; i < tempData.length; i++) {
            mapData.put(tempField[i], tempData[i]);
        }
        TempMessage tempMessage = new TempMessage(mapData);
        if (StringUtils.isNotBlank(url)) {
            tempMessage.setUrl(url);
        }
        WxMpTemplate wxMpTemplate = getTemplateByTitle(wxMpService, title);
        tempMessage.setTempId(wxMpTemplate.getTemplateId());
        tempMessage.setTempName(wxMpTemplate.getTitle());
        tempMessage.setToUser(openId);
        WxMpTemplateMessage tm = TempMsgUtil.createTemp(tempMessage, tempField, tempColor);
        wxMpService.getTemplateMsgService().sendTemplateMsg(tm);

    }

    /**
     * 发送Email消息
     *
     * @param toUser  用户Email地址
     * @param title   Email标题
     * @param content Email内容
     * @return
     */
    public static String remindByEmail(String toUser, String title, String content) {

        String wsUrl = "http://haois.me/ws";
//        wsUrl = "http://localhost:8090";
        String url = wsUrl + "/api/web/email/send";
        Map<String, String> params = new HashMap<>(5);
        params.put("toUser", toUser);
        params.put("title", title);
        params.put("content", content);
        String response = HttpClientUtil.sendPost(url, params, null);
        return response;
    }

    /**
     * 发送客服消息
     *
     * @param wxMpService
     * @param toUserOpenId 目标用户OpenId
     * @param content      内容
     * @return
     */
    public static boolean remindByKeFu(WxMpService wxMpService, String toUserOpenId, String content) {
        WxMpKefuMessage message = new WxMpKefuMessage();
        message.setMsgType(WxConsts.KefuMsgType.TEXT);
        message.setToUser(toUserOpenId);
        message.setContent(content);
        try {
            boolean result = wxMpService.getKefuService().sendKefuMessage(message);
            return result;
        } catch (WxErrorException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 根据模板名获取模板详情
     *
     * @param wxMpService
     * @param title
     * @return
     * @throws WxErrorException
     */
    public static WxMpTemplate getTemplateByTitle(WxMpService wxMpService, String title)
            throws WxErrorException {
        List<WxMpTemplate> result = wxMpService.getTemplateMsgService().getAllPrivateTemplate();
        return TempMsgUtil.getTemplateByTitle(result, title);
    }
}
